package storageLayer

import (
	"hotstuffImpl/entity"
	"sync"
)

type storage struct {
	mut sync.RWMutex
	// 先用map来存储block
	blocks map[entity.Hash]*entity.Block
}

// Store 将block存储到存储层中
func (s *storage) Store(block *entity.Block) {
	//TODO: 验证逻辑先省略
	s.mut.Lock()
	defer s.mut.Unlock()

	s.blocks[block.Hash] = block
}

func (s *storage) Get(hash entity.Hash) *entity.Block {
	s.mut.RLock()
	defer s.mut.RUnlock()

	return s.blocks[hash]
}

// 仅返回一个存储层管理器
var once sync.Once

func GetStoage() *storage {
	var instance *storage

	once.Do(func() {
		instance = &storage{
			blocks: make(map[entity.Hash]*entity.Block),
		}
	})
	return instance
}
